package com.yupi.yuaiagent.controller;

import com.yupi.yuaiagent.App.LoveApp;
import com.yupi.yuaiagent.agent.CanManus;
import io.reactivex.Emitter;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.io.IOException;

@RestController
@RequestMapping("/ai")
public class AiController {


    @Resource
    private LoveApp loveApp;

    /**
     * 恋爱大师APP同步调用
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping("/love_app/chat/sync")
    public String doChatWithLoveAppSync(String message,String chatId)
    {
        return loveApp.doChat(message,chatId);
    }


    /**
     * 恋爱大师APP流式调用方式一 注解需要指定produces属性
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping(value = "/love_app/chat/stream",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> doChatWithLoveAppStream(String message, String chatId)
    {
        return loveApp.doChatWithStream(message, chatId);
    }

    /**
     * 恋爱大师APP流式调用方式二
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping(value = "/love_app/chat/sse",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> doChatWithLoveAppSse(String message, String chatId)
    {
        return loveApp.doChatWithStream(message, chatId)
                .map(chunk ->
                    ServerSentEvent.<String>builder()
                            .data(chunk)
                            .build()
                );
    }


    /**
     * 恋爱大师APP流式调用方式三
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping(value = "/love_app/chat/sse/emitter")
    public SseEmitter doChatWithLoveAppSseEmitter(String message, String chatId)
    {
        // new 一个Sse推送对象 指定连接超时时间
        SseEmitter emitter = new SseEmitter(10000L);
        // 获取Flux数据流并且订阅
        loveApp.doChatWithStream(message, chatId)
                // 处理每条消息
                .subscribe(chunk -> {
                    try {
                        emitter.send(chunk);
                    } catch (IOException e) {
                        emitter.completeWithError(e);
                    }
                }// 处理错误
                ,emitter::completeWithError
                 // 处理完成
                ,emitter::complete);
        // 返回emitter
        return emitter;
    }

    @Resource
    private ToolCallback[] allTools;
    @Resource
    ChatModel dashscopeChatModel;

    /**
     * 流式调用 CanManus超级智能体
     * @param message 用户输入
     * @return SseEmitter 实例
     */
    @GetMapping(value = "/manus/chat")
    public SseEmitter doChatWithManus(String message)
    {
        CanManus canManus = new CanManus(allTools, dashscopeChatModel);
        return canManus.runStream(message);
    }



}
